package com.whpost.api.controller.sys;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.whpost.api.utils.ApiUtils;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.t.FileService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@RestController
@RequestMapping("/sys/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 下载文件，根据前台传的token来鉴权
     * @return
     */
    @RequestMapping(value ="/download")
    public String download(String fileId, String token,HttpServletResponse response) throws IOException {
        String loginId  = (String)StpUtil.getLoginIdByToken(token);
        return fileService.downloadById(loginId,fileId,response);
    }

    /**
     * 下载APK
     * @return
     */
    @RequestMapping(value ="/download_apk")
    public String downloadApk(HttpServletResponse response) throws IOException {
        return fileService.downloadApk(response);
    }
}
